<template>
    <div>
        <el-form

                v-loading="loading"
                element-loading-text="正在登录中"
                element-loading-spinner="el-icon-loading"
                element-loading-background="rgba(0, 0, 0, 0.8)"

                :rules="rules"
                ref="loginForm"
                :model="loginForm"
                class="loginContainer">
            <h3 class="loginTitle">系统登录</h3>
<!--            可以不加prop属性，因为加了required校验-->
            <el-form-item prop="username">
                <el-input size="normal" type="text" v-model="loginForm.username" auto-complete="off" aria-placeholder="请输入用户名"></el-input>
            </el-form-item>
            <el-form-item prop="password">
                <el-input size="normal" type="password" v-model="loginForm.password" auto-complete="off" aria-placeholder="请输入密码" @keydown.enter.native="submitLogin"></el-input>
            </el-form-item>
            <el-checkbox class="loginRemember" v-model="checked"></el-checkbox>
            <el-button size="normal" type="primary" style="width: 100%;" @click="submitLogin">登录</el-button>
        </el-form>
    </div>
</template>

<script>
    //导入自定义登录方法(在main.js中引用插件，此处就不用每次都去导入了)
    //import {postKeyValueRequest} from "../tuils/api";

    export default {
        name: "Login",
        data() {
            return {
                loading:false,
                loginForm:{
                    username:'admin',
                    password:'123'
                },
                checked:true,
                rules:{
                    username:[{required:true,message:'请输入用户名',trigger:'blur'}],
                    password:[{required:true,message:'请输入密码',trigger:'blur'}]
                }
            }
        },

        methods:{
            submitLogin(){
                this.$refs.loginForm.validate((valid) => {
                    if (valid) {
                        this.loading = true;
                        /**
                         * @description:
                         * @date: 202105-11 20:15:39
                         */
                        //调用自定义登录方法
                        //如果没用引用插件的情况下，此处就省略this.
                        this.postKeyValueRequest('/doLogin',this.loginForm).then(resp=>{
                            this.loading = false;
                            if (resp) {
                                this.$store.commit('INIT_CURRENTHR', resp.obj);
                                //保存用户的数据
                                window.sessionStorage.setItem("user",JSON.stringify(resp.obj));
                                /**
                                 * @description: 未登录时输入某地址后，获取地址，实现登录后跳转至前面输入的地址
                                 * @date: 2021-05-18 21:16:00
                                 */
                                let path = this.$route.query.redirect;
                                //页面跳转
                                //$router: 获取你当前vue对象中的对象(在main.js中)
                                //replace：不能点击home按钮(即返回上一页)
                                this.$router.replace((path == '/' || path == undefined)?'/home':path)
                            }
                        })
                        // alert('submit!');
                    } else {
                        this.$message.error("请输入用户名和密码");
                        return false;
                    }
                });
            }
        }
    }
</script>

<style>
    .loginContainer{
        border-radius: 15px;
        background-clip: padding-box;
        margin: 180px auto;
        width: 350px;
        padding: 15px 35px 15px 35px;
        background: #ffffff;
        border: 1px solid #eaeaea;
    /*    阴影效果*/
        box-shadow: 0 0 25px #cac6c6;
    }
    .loginTitle{
        /*上 右 下 左*/
        margin: 15px auto 20px auto;
        text-align: center;
        color: #505458;
    }
    .loginRemember{
        text-align: left;
        margin: 0px 0px 15px 0px;
    }
</style>